#!/usr/bin/env python3
"""
Streamlit launcher for ChatWithRepository
"""

import os
import sys
import subprocess
from pathlib import Path

def main():
    """Launch the Streamlit application."""
    
    # Check if streamlit is installed
    try:
        import streamlit
    except ImportError:
        print("❌ Streamlit not found. Installing...")
        subprocess.run([sys.executable, "-m", "pip", "install", "streamlit"])
    
    # Set environment variables
    os.environ["STREAMLIT_RUNONSAVE"] = "true"
    os.environ["STREAMLIT_SERVER_HEADLESS"] = "false"
    
    # Launch Streamlit
    script_path = Path(__file__).parent / "streamlit_app.py"
    
    print("🚀 Launching ChatWithRepository Streamlit app...")
    print("📱 Open http://localhost:8501 in your browser")
    print("📝 Press Ctrl+C to stop the server")
    
    try:
        subprocess.run([
            sys.executable, "-m", "streamlit", "run", 
            str(script_path),
            "--server.port=8501",
            "--server.address=0.0.0.0"
        ])
    except KeyboardInterrupt:
        print("\n👋 Stopping server...")
    except Exception as e:
        print(f"❌ Error: {e}")
        print("💡 Try running: streamlit run streamlit_app.py")

if __name__ == "__main__":
    main()